package com.qf.controller;


import com.qf.entity.PageBean;
import com.qf.entity.Profile;
import com.qf.entity.User;
import com.qf.service.ProfileService;
import com.qf.service.impl.ProfileServiceImpl;
import com.qf.utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet(name = "ProfileServlet", urlPatterns = "/profilelist")
public class ProfileServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            response.sendRedirect("login.jsp");
            return;
        }

        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        //默认页码 第一页
        int pn = 1;
        //默认页大小 10
        int ps = 10;
        if (!StringUtils.isEmpty(pageNum)) {
            try {
                pn = Integer.parseInt(pageNum);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (pn < 1) {
                pn = 1;
            }
        }
        if (!StringUtils.isEmpty(pageSize)) {
            try {
                ps = Integer.parseInt(pageSize);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (ps < 1) {
                ps = 10;
            }
        }
        ProfileService profileService = new ProfileServiceImpl();

        PageBean<Profile> pageBean = profileService.findByPage(pn, ps);

        request.setAttribute("pageBean", pageBean);

        request.getRequestDispatcher("/list.jsp").forward(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
